﻿package OracleHelper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleHelper {
	/* ����driver,url,user,pwd�� */
	// oracle.jdbc.OracleDriver
	private static String driver = "oracle.jdbc.driver.OracleDriver";
	//private static String url = "jdbc:oracle:thin:@localhost:1521:orcl", user = "c##scott_20161946", pwd = "20161946";
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl", user = "c##university_resource", pwd = "20161946";
	private static Connection con;
	static {
		try {
			Class.forName(driver);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/* 执行更新操作 */
	public static int executeUpdate(String sql) {
		int r=0;
		try {
			con = DriverManager.getConnection(url, user, pwd);
			Statement cmd = con.createStatement();
			r=cmd.executeUpdate(sql);
			con.close();
		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return r;
	}
	
	/*执行select语句，返回唯一值*/
	public static Object executeSingleQuery(String singleSql) {
		Object r=null;
		ResultSet rs=null;
		try {
			Connection conn = DriverManager.getConnection(url, user, pwd);  //数据库连接
			Statement cmd = conn.createStatement();      //数据库操作,用于执行静态的SQL语句并返回它所生成结果的对象
			rs = cmd.executeQuery(singleSql);
			if (rs.next())
				r = rs.getObject(1);
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return r;
	}

	/*查询*/
	public static ResultSet executeQuery(String sql) {
		ResultSet rs = null;
		try {
			con = DriverManager.getConnection(url, user, pwd);
			Statement cmd = con.createStatement();
			rs = cmd.executeQuery(sql);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return rs;
	}

	/* 关闭连接 */
	public static void closeConnection() {
		try {
			if (!con.isClosed())
				con.close();
		} catch (Exception ex) {
		}
	}

}
